package Maps 
{
	import com.greensock.TweenLite;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;	
	/**
	 * ...
	 * @author Serg de Adelantado
	 */
	public class CommonTooltip extends Sprite 
	{		
		static public const COLOR:uint = 0xE5E6C4;
		protected var FWidth:int;	
		protected var FHeight:int;		
		protected var FQuad:Sprite;
		protected var FLBCorner:Sprite;
		protected var FRBCorner:Sprite;
		protected var FRCorner:Sprite;
		protected var FLCorner:Sprite;		
		protected var FVisible:Boolean;
		protected var FFlipH:Boolean;
		protected var FFlipV:Boolean;
		
		public function CommonTooltip() 
		{
			addEventListener(Event.ENTER_FRAME, OnEnterFrame);
			FWidth = 128;
			FHeight = 64;			
			FLBCorner = new Sprite();
			addChild(FLBCorner);			
			FQuad = new Sprite();			
			addChild(FQuad);
			FRBCorner = new Sprite();			
			addChild(FRBCorner);
			FRCorner = new Sprite();			
			addChild(FRCorner);
			FLCorner = new Sprite();			
			addChild(FLCorner);
			Redraw();
			
			FLBCorner.graphics.beginFill(COLOR)
			FLBCorner.graphics.lineTo(16, 0);
			FLBCorner.graphics.lineTo(0, 16);
			FLBCorner.graphics.lineTo(0, 0);
			
			FRBCorner.graphics.beginFill(COLOR)
			FRBCorner.graphics.lineTo(16, 16);
			FRBCorner.graphics.lineTo(16, 0);
			FRBCorner.graphics.lineTo(0, 0);
			
			FRCorner.graphics.beginFill(COLOR)
			FRCorner.graphics.lineTo(16, 8);
			FRCorner.graphics.lineTo(0, 16);
			FRCorner.graphics.lineTo(0, 0);	
			
			FLCorner.graphics.beginFill(COLOR)
			FLCorner.graphics.lineTo(16, -8);
			FLCorner.graphics.lineTo(16, 8);
			FLCorner.graphics.lineTo(0, 0);
			
			mouseEnabled = mouseChildren = false;
			alpha = 0;
			FVisible = false;
			FFlipH = false;
			FFlipV = false;
		}
			
		protected function OnEnterFrame(e:Event):void 
		{
			if (FVisible && parent)
			{
				var vX = parent.mouseX;
				var vY = parent.mouseY;
				
				var vRight:Number = stage.stageWidth - vX;				
				var vTop:Number = vY - height;				
				if (vRight >= FWidth + 16)				
				{
					x = vX;
					FFlipH = false;					
				}
				else
				{
					x = vX - FWidth;
					FFlipH = true;
				}
				if (vTop <= 0)
				{
					vTop = 0;
					FFlipV = true;
				}
				else
				{					
					FFlipV = false;
				}
				y = vTop;				
				FLBCorner.visible = !FFlipH && !FFlipV;
				FRBCorner.visible = FFlipH && !FFlipV;
				FRCorner.visible = FFlipH && FFlipV;
				FLCorner.visible = FFlipV && !FFlipH;
				if (FRCorner.visible)
				{
					FRCorner.y = Math.min(mouseY, FHeight - 16);
					x -= 16;
				}
				if (FLCorner.visible)
				{
					FLCorner.y = Math.min(mouseY, FHeight - 16) + 8;
					x += 16;
				}
			}
		}
		
		override public function set visible(value:Boolean):void 
		{
			FVisible = value;
			TweenLite.killTweensOf(this);
			if (value)
			{				
				TweenLite.to(this, 0.25, { alpha:1 } );
			}
			else
			{
				TweenLite.to(this, 0.25, { alpha:0 } );
			}
		}
		
		public function SetSize(AWidth:int, AHeight:int):void 
		{
			FWidth = AWidth;
			FHeight = AHeight;
			Redraw();
		}
		
		protected function Redraw():void 
		{
			FQuad.graphics.clear();
			FQuad.graphics.beginFill(COLOR)
			FQuad.graphics.lineTo(FWidth, 0);
			FQuad.graphics.lineTo(FWidth, FHeight);
			FQuad.graphics.lineTo(0, FHeight);
			FQuad.graphics.lineTo(0, 0);
			
			FLBCorner.y = FHeight;						
			FRBCorner.y = FHeight;
			FRBCorner.x = FWidth - 16;			
			FRCorner.x = FWidth;								
			FLCorner.x = -16;
		}
	}
}